home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 31
/
Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso
/
Aminet
/
util
/
misc
/
RandPY.lha
/
RandPinYin
/
Original
/
randpy.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-21
|
4KB
|
177 lines
/*
RANDPY by Huangxin Wang
Dept. of Phyics, University of Pennsylvania
wang@pennmess.physics.upenn.edu
June 20, 1991
"randpy" randomly selects a Chinese character and display it
in Chinese as well as its pinyin. You can learn to read
correct Chinese pronunciation at your lunch time, or put
it in your .logout to leave a Chinese character on the screen!
You will find out that there are so many Chinese characters that
you know the meaning but pronounce it wrong.
To install:
make randpy
then put pinyin.hz in the directory defined by environment
variable HZDIR (used by "hzview").
To run it, you need the hzview program by Fung F. Lee
(available by anonymous ftp: ahkcus.org [192.55.187.25])
randpy | hzview
or
randpy -b | hzview
Credit:
pinyin.hz table is edited by Ed Lai. I prefer to use pinyin.hz
rather than pinyin.gb, since it is editable.
hz->gb part is from Fung F. Lee's hanzi package.
*/
#include <stdio.h>
#define PINYIN "pinyin.hz"
#define HZDIR "HZDIR"
#define PINYIN_NUMS 410 /* total number of pinyin items */
#define TONE_NUMS 5 /* number of tones: from 1 to 5 */
#define MAXINT 2147483647.0 /* 2**31 - 1 for the maximum of rand() */
/* for System V, MAXINT=32767=2**15-1 */
#define ASCII1 0xa3 /* ASCII Chinese font high byte */
#define ASCII2 0xc1 /* ASCII Chinese font low byte for 'A' */
#define isTone(c) ((c)=='1' || (c)=='2' || (c)=='3' || (c)=='4' || (c)=='5')
char py_tab[100];
FILE *pinyin;
char s[200], hz[200];
main(argc, argv)
int argc;
char *argv[];
{
int rpy, rtone, i;
int c1, c2;
int big_ascii = 0; /* flag for whether big font for ASCII */
srand((int)time(0L)); /* initialize the randomizer seed */
for (i=1; i<argc; i++) {
if (strcmp("-b", argv[i]) ==0) big_ascii = 1;
}
/* first, assume pinyin.hz is in current dir
*/
if ((pinyin = fopen(PINYIN, "r")) == NULL) {
if (getenv(HZDIR) == NULL) {
fprintf(stderr, "Please setenv %s appropriately.\n",HZDIR);
exit (1);
}
strcpy(py_tab, getenv(HZDIR));
strcat(py_tab,"/");
strcat(py_tab,PINYIN);
if ((pinyin = fopen(py_tab, "r")) == NULL) {
fprintf(stderr,"Cannot open pinyin table %s\n", PINYIN);
exit(-1);
}
}
/* read off the header lines */
/* first useful line is 'A,A,A" */
do {
fgets(s, 200, pinyin); /* fgets() get a string upto a <CR> */
} while (strcmp("A,A,A\n", s) != 0); /* \n is needed */
/* get a random pinyin */
rpy = PINYIN_NUMS * (rand() / MAXINT) + 1;
rpy = (rpy > PINYIN_NUMS ? PINYIN_NUMS : rpy);
i = 0;
while (++i < rpy) {
do {
fgets(s, 200, pinyin);
} while (isTone(s[0]));
}
/* Now we get the pinyin, choose a random tone */
rtone = TONE_NUMS * (rand() / MAXINT) + 1;
rtone = (rtone > TONE_NUMS ? TONE_NUMS : rtone);
/* debugging:
fprintf(stderr, "PY = %d tone = %d\n", rpy, rtone);
*/
for (i=0; i<rtone; i++) {
c1 = getc(pinyin);
/* if this pinyin does not have that
tone, then keep the last one */
ungetc(c1, pinyin);
if (isTone(c1)) fgets(hz, 200, pinyin);
else {
rtone = i;
break;
}
}
/* get the number of characters in the list of same pinyin and tone */
/* Two chars in hz makes a chinese, the first three characters and
the last two should be exclude here is an example of the list:
1~{1_1`1^l.lTm>rysVv}~}
*/
i = (strlen(hz) - 5) /2;
/* Now we get a random characters */
i = i * (rand() / MAXINT);
/* Again 2 bytes as a chinese character, first 3 chars excluded */
i = 3 + 2*i;
/* Now print the Chinese character: */
gb2dos(hz[i], hz[i+1], &c1, &c2);
fputc(c1, stdout);
fputc(c2, stdout);
if (!big_ascii) {
/* Now print the pinyin and tone in small terminal character: */
fprintf(stdout," ");
for(i=0; s[i] != ','; i++) {
fputc(s[i], stdout);
}
/* label the tone, maybe different from rtone */
fputc(hz[0], stdout);
}
else {
/* Now print the pinyin and tone in big Chinese font: */
fputc('\n', stdout);
for(i=0; s[i] != ','; i++) {
fputc(ASCII1, stdout);
fputc(ASCII2+s[i]-'A', stdout);
}
fputc(ASCII1, stdout);
fputc(ASCII2+hz[0]-'A', stdout);
}
/* Now write the original PinYin, Wade-Gile, and Yale */
fprintf(stdout," %s",s);
fclose(pinyin);
}
/*
hz2gb: convert a HZ file into a Macintosh/CCDOS GB file. By Fung F. Lee
*/
gb2dos(hi, lo, hi1, lo1)
int hi, lo, *hi1, *lo1;
{
*hi1 = 0x80 + hi;
*lo1 = 0x80 + lo;
}